﻿using FangChanCrm.Services.Interface;
using FangChanCrmEF;
using Maticsoft.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace FangChanCrm.Services.Realization
{
    public class t_ZhanDuiBiaoService : It_ZhanDuiBiaoService
    {
        public readonly MyContext _Context;
        public t_ZhanDuiBiaoService(MyContext myContext)
        {
            _Context = myContext;
        }

        public int Add(t_ZhanDuiBiao t_ZhanDuiBiao)
        {
            _Context.t_ZhanDuiBiao.Add(t_ZhanDuiBiao);
            return _Context.SaveChanges();
        }

        public t_ZhanDuiBiao GetId(int id)
        {
            return _Context.t_ZhanDuiBiao.FirstOrDefault(x => x.id == id);
        }

        public List<t_ZhanDuiBiao> GetList()
        {
            return _Context.t_ZhanDuiBiao.Include(x => x.t_ZhanQuBiao).Include(x => x.t_RenYuanBiaos).ToList();
        }
        public bool JinYong(int id)
        {
            throw new NotImplementedException();
        }

        public bool QiYoong(int id)
        {
            throw new NotImplementedException();
        }

        
        public string ReturnWhere(t_ZhanDuiBiao t_ZhanDuiBiao)
        {
            string returnstr = "id != 1";
            if (t_ZhanDuiBiao.BeiZhu1 != null)
            {
                returnstr = returnstr + " and BeiZhu1.Contains(" + t_ZhanDuiBiao.BeiZhu1 + ")";
            }
            if (t_ZhanDuiBiao.BeiZhu2 != null)
            {
                returnstr = returnstr + " and BeiZhu2.Contains(" + t_ZhanDuiBiao.BeiZhu2 + ")";
            }
            if (t_ZhanDuiBiao.BeiZhu3 != null)
            {
                returnstr = returnstr + " and BeiZhu3.Contains(" + t_ZhanDuiBiao.BeiZhu3 + ")";
            }
            if (t_ZhanDuiBiao.BeiZhu4 != null)
            {
                returnstr = returnstr + " and BeiZhu4.Contains(" + t_ZhanDuiBiao.BeiZhu4 + ")";
            }
            if (t_ZhanDuiBiao.BeiZhu5 != null)
            {
                returnstr = returnstr + " and BeiZhu5.Contains(" + t_ZhanDuiBiao.BeiZhu5 + ")";
            }
            if (t_ZhanDuiBiao.BeiZhu6 != null)
            {
                returnstr = returnstr + " and BeiZhu6.Contains(" + t_ZhanDuiBiao.BeiZhu6 + ")";
            }
            if (t_ZhanDuiBiao.BeiZhu7 != null)
            {
                returnstr = returnstr + " and BeiZhu7.Contains(" + t_ZhanDuiBiao.BeiZhu7 + ")";
            }
            if (t_ZhanDuiBiao.BeiZhu8 != null)
            {
                returnstr = returnstr + " and BeiZhu8.Contains(" + t_ZhanDuiBiao.BeiZhu8 + ")";
            }
            if (t_ZhanDuiBiao.BeiZhu9 != null)
            {
                returnstr = returnstr + " and BeiZhu9.Contains(" + t_ZhanDuiBiao.BeiZhu9 + ")";
            }
            if (t_ZhanDuiBiao.BeiZhu10 != null)
            {
                returnstr = returnstr + " and BeiZhu10.Contains(" + t_ZhanDuiBiao.BeiZhu10 + ")";
            }
            if (t_ZhanDuiBiao.ZhanDuiMingCheng != null && t_ZhanDuiBiao.ZhanDuiMingCheng != "")
            {
                returnstr = returnstr + " and ZhanDuiMingCheng.Contains(\"" + t_ZhanDuiBiao.ZhanDuiMingCheng + "\")";
            }
            if (t_ZhanDuiBiao.ZhanQuId != 0)
            {
                returnstr = returnstr + " and ZhanQuId = " + t_ZhanDuiBiao.ZhanQuId + "";
            }
            if (t_ZhanDuiBiao.ZhuangTai != "0")
            {
                returnstr = returnstr + " and ZhuangTai.Contains(\"" + t_ZhanDuiBiao.ZhuangTai + "\")";
            }
            return returnstr;
        }

        public List<t_ZhanQuBiao> GetZhanQuList()
        {
            return _Context.t_ZhanQuBiao.Where(x => x.ZhuangTai == "启用").ToList();
        }

        public string GetListWithFenYe(t_ZhanDuiBiao t_ZhanDuiBiao, int start, int limit, string dir, int column)
        {
            var returnwhere = ReturnWhere(t_ZhanDuiBiao);
            var list = new List<t_ZhanDuiBiao>();
            if (returnwhere.Length > 0)
            {
                list = _Context.t_ZhanDuiBiao.Include(x => x.t_ZhanQuBiao).Where(returnwhere).ToList();
            }
            else
            {
                list = _Context.t_ZhanDuiBiao.Include(x => x.t_ZhanQuBiao).ToList();
            }
            switch (column)
            {
                case 0:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => x.ZhanDuiMingCheng).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => x.ZhanDuiMingCheng).ToList();
                    }
                    break;
                case 1:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => x.t_ZhanQuBiao.ZhanQuMingCheng).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => x.t_ZhanQuBiao.ZhanQuMingCheng).ToList();
                    }
                    break;
                case 2:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => x.ZhuangTai).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => x.ZhuangTai).ToList();
                    }
                    break;
                default:
                    break;
            }
            return Helper.ListToJsonAddFenYe(list, start, limit);
        }
    }
}
